package com.qs.user.ui.VM;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;

import com.qs.user.BR;
import com.qs.user.R;
import com.qs.base.data.IdentifyHistoricalEntity;

import java.io.File;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 识别历史vm
 *
 * @Author ltzz
 * @Date 2019/8/24
 */
public class IdentifyHistoricalViewModel extends BaseViewModel {
    public IdentifyHistoricalViewModel(@NonNull Application application) {
        super(application);
    }

    //识别历史文件
    public List<IdentifyHistoricalEntity> imgs;

    //返回
    public BindingCommand backClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });
    //清空
    public BindingCommand cleanClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            observableList.clear();
            if (imgs != null && imgs.size() > 0) {
                for (int i = 0; i < imgs.size(); i++) {
                    if (imgs.get(i).getFiles() != null && imgs.get(i).getFiles().size() > 0) {
                        for (int j = 0; j < imgs.get(i).getFiles().size(); j++) {
                            File file = new File(imgs.get(i).getFiles().get(j));
                            file.delete();
                        }
                    }
                }
                ToastUtils.showShort("清除成功");
            } else
                ToastUtils.showShort("无文件");
        }
    });


    //给RecyclerView添加ObservableList
    public ObservableList<IdentifyHistoricalItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<IdentifyHistoricalItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<IdentifyHistoricalItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, IdentifyHistoricalItemViewModel item) {
            //设置布局
            if (!StringUtils.isTrimEmpty(item.mItemEntity.get().getDate()))
                itemBinding.set(BR.viewModel, R.layout.user_identify_historical_date_item);
            else
                itemBinding.set(BR.viewModel, R.layout.user_identify_historical_img_item);
        }
    });
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<IdentifyHistoricalItemViewModel> adapter = new BindingRecyclerViewAdapter<IdentifyHistoricalItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final IdentifyHistoricalItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
        }
    };
}
